package com.xmsx.back.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.xmsx.back.results.Result;
import com.xmsx.back.utils.JWTUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("utf-8");
        String token = request.getHeader("Authorization");
        if (null != token) {
            //验证token是否正确
            boolean result = JWTUtil.verify(token);
            if (result) {
                return true;
            }
        }
        response.getWriter().write(JSONObject.toJSONString(Result.error("token校验失败")));
        return false;

    }
}
